package com.loveyukari.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.loveyukari.model.Room;
import com.loveyukari.service.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loveyukari.common.exception.InternalException;
import com.loveyukari.model.Liwu;
import com.loveyukari.model.XmMessage;
import com.loveyukari.support.JsonResult;
import com.loveyukari.utils.HttpUtils;
import com.loveyukari.utils.PinyinUtils;

/**
 *
 * yangshaohua 2016年12月5日下午5:12:51
 **/

@RequestMapping("/admin/douyu")
@Controller
public class DouyuController {

	@Autowired
	private LiwuService liwuService;
	
	@Autowired
	private ServerService serverService;
	
	@Autowired
	private XiaomaService xiaomaService;
	@Autowired
	private RoomService roomService;
	@Autowired
	private ManagerService managerService;
	
	private String socketUrl;

	private Logger log = LoggerFactory.getLogger(DouyuController.class);
	
	@RequestMapping("/liwu")
	public String liwu(Model model, HttpServletRequest request, HttpServletResponse response) {
		String sign = (String) request.getAttribute("sign");
		int authority = managerService.getManageAuthority(sign);
		if (authority > 5){
			return "kb";
		}
		XmMessage xmMessage = xiaomaService.getMessageByXmLast("小马");
		List<Integer> list = liwuService.getHideLiwuStyleIds();
		model.addAttribute("xmMessage", xmMessage);
		model.addAttribute("hideIds", list);
		return "douyu/liwu";
	}
	
	@RequestMapping("/getXmMessage")
	@ResponseBody
	public JsonResult<Object> getXmMessage(){
		XmMessage xmMessage = xiaomaService.getMessageByXmLast("小马");
		return new JsonResult<Object>(0,"",xmMessage);
	}
	
	@RequestMapping("/changeServer")
	@ResponseBody
	public JsonResult<Object> changeServer(){
		String host = serverService.changeServer();
		return new JsonResult<Object>(0,"",host);
	}



	@RequestMapping("/getLiwu")
	@ResponseBody
	public List<Liwu> getLiwu(@RequestParam(defaultValue="-1")Integer size) {
		List<Liwu> liwu = liwuService.getLiwu(size);
		return liwu;
	}
	
	@RequestMapping("/chatmsg")
	public String chatmsg(Model model,HttpServletRequest request,Integer fontSize) {
		String serverName = request.getHeader("Host");
		if (StringUtils.isBlank(serverName)){
			serverName = request.getServerName();
		}
		int port = request.getServerPort();
		if(port == 80 || port == 443){
			model.addAttribute("socketUrl","wss://"+serverName+"/yukari/webSocket/yukari");
		} else {
			model.addAttribute("socketUrl","ws://"+serverName+"/yukari/webSocket/yukari");
		}
		if (fontSize != null){
			model.addAttribute("fontSize",fontSize);
		}
		return "douyu/chatmsg";
	}

	@RequestMapping("/m/chatmsg")
	public String mChatmsg(Model model,HttpServletRequest request,Integer fontSize) {
		String serverName = request.getHeader("Host");
		if (StringUtils.isBlank(serverName)){
			serverName = request.getServerName();
		}
		int port = request.getServerPort();
		if(port == 80 || port == 443){
			model.addAttribute("socketUrl","wss://"+serverName+"/yukari/webSocket/yukari");
		} else {
			model.addAttribute("socketUrl","ws://"+serverName+"/yukari/webSocket/yukari");
		}
		if (fontSize != null){
			model.addAttribute("fontSize",fontSize);
		}
		return "m/chatmsg";
	}




	@RequestMapping("/closeLiwu")
	@ResponseBody
	public JsonResult<Object> closeLiwu(@RequestParam Integer[] ids) {
		List<Integer> list = new ArrayList<Integer>();
		Collections.addAll(list, ids);
		liwuService.closeLiwu(list);
		return new JsonResult<Object>(0, JsonResult.MESSAGE_OK);
	}

	@RequestMapping("/markRead")
	@ResponseBody
	public JsonResult<Object> markRead(int id) {
		liwuService.markRead(id);
		return new JsonResult<Object>(0, JsonResult.MESSAGE_OK);
	}

	@RequestMapping("/reloadStyle")
	@ResponseBody
	public JsonResult<Object> reloadStyle() {
		liwuService.reloadStyleMap();
		return new JsonResult<Object>(0, JsonResult.MESSAGE_OK);
	}

	@RequestMapping("/addLiwuStyle")
	@ResponseBody
	public JsonResult<Object> addLiwuStyle() {
		liwuService.addLiwuStyle();
		return new JsonResult<Object>(0, JsonResult.MESSAGE_OK);
	}

	@RequestMapping("/pinyin")
	@ResponseBody
	public JsonResult<Object> pinyin(String str) {
//		List<String> list = new ArrayList<String>();
//		for (int i = 0; i < str.length(); i++) {
//			char c = str.charAt(i);
//			String pinyin = PinyinUtils.getPinyinByChar(c);
//			list.add(pinyin);
//		}
		String pingyin = PinyinUtils.getPingyin(str);
		return new JsonResult<Object>(0, "", pingyin);
	}
	
	@RequestMapping("/getRoomStatus")
	@ResponseBody
	public JsonResult<Object> getRoomStatus(){
		Room room = roomService.getMapRoom(196);
		return new JsonResult<Object>(0,"",room);
	}

	@RequestMapping("/getRoomRank")
	@ResponseBody
	public JsonResult getRoomRank() throws Exception {
		String result = HttpUtils.sendGet("http://open.douyucdn.cn/api/RoomApi/live/0");
		JSONObject json = JSONObject.fromObject(result);
		if(json.optInt("error",-1) == 0){
			return new JsonResult(0,"",json.optJSONArray("data"));
		} else {
			return new JsonResult(-1,"");
		}
	}

	@RequestMapping("/setHideIds")
    @ResponseBody
    public JsonResult setHideIds(Integer[] ids){
	    liwuService.setHideLiwuStyleIds(ids);
	    return new JsonResult(0,"");
    }
	

}
